package com.pda.gongpin.ui.handingin;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.pda.gongpin.R;
import com.pda.gongpin.bean.ScanBean;
import com.pda.gongpin.bean.ScanResultBean;
import com.pda.gongpin.bean.SkuBean;
import com.pda.gongpin.bean.request.EntryNoRequest;
import com.pda.gongpin.bean.request.HandingInRequest;
import com.pda.gongpin.bean.request.QRcodeRequest;
import com.pda.gongpin.contract.HandingInContract;
import com.pda.gongpin.dialog.AffirmDialog;
import com.pda.gongpin.presenter.HandingInPresenter;
import com.pda.gongpin.utils.PopUtisl;
import com.pda.gongpin.view.ScanButView;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class HandingInFragment extends BaseMvpFragment<HandingInPresenter> implements HandingInContract.View {
    @BindView(R.id.tv_deliveryNo)
    TextView tvDeliveryNo;
    @BindView(R.id.tv_sku)
    TextView tvSku;
    @BindView(R.id.tv_articleId)
    TextView tvArticleId;
    @BindView(R.id.tv_spec)
    TextView tvSpec;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.tv_waitStackNum)
    TextView tvWaitStackNum;
    @BindView(R.id.tv_storageBin)
    TextView tvStorageBin;
    @BindView(R.id.tv_pallet)
    TextView tvPallet;
    @BindView(R.id.et_num)
    EditText etNum;
    @BindView(R.id.sb_scan)
    ScanButView sbScan;
    @BindView(R.id.tv_move_out)
    TextView tvMoveOut;
    @BindView(R.id.et_num_jian)
    EditText etNumJian;
    @BindView(R.id.tv_caseUnit)
    TextView tvCaseUnit;
    @BindView(R.id.tv_innerUnit)
    TextView tvInnerUnit;
    private SkuBean skuBean;
    private List<SkuBean> skuBeanList = new ArrayList<>();

    public HandingInFragment(String entryNo) {
        this.entryNo = entryNo;
    }

    public HandingInFragment() {
    }

    private String entryNo;

    @Override
    public HandingInPresenter getPresenter() {
        return new HandingInPresenter();
    }

    @Override
    public int getViewId() {
        return R.layout.fragment_handing_in;
    }

    @Override
    public void initView() {
        tvDeliveryNo.setText("入库单信息 " + entryNo);
        presenter.getEntryNumber(new EntryNoRequest(entryNo));
    }

    @Override
    public void initLinsenterner() {
        sbScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                QRcodeRequest request = new QRcodeRequest();
                request.setQRCode(scanBean.getCode());
                presenter.scanTheQRCodeToVerify(request);
            }
        });
    }

    @Override
    public void getData() {

    }

    @Override
    public void setList(List<SkuBean> list) {
        skuBeanList.addAll(list);
        if (skuBeanList.size() > 0) {
            skuBean = skuBeanList.get(0);
            init();
        }
    }

    private void init() {
        if (skuBean != null) {
            tvSku.setText(skuBean.getSku());
            tvArticleId.setText("商品编码：" + skuBean.getArticleId());
            tvSpec.setText("规格：   " + skuBean.getSpec());
            tvDesc.setText("商品名称：" + skuBean.getDesc());
            tvCaseUnit.setText(skuBean.getCaseUnit());
            tvInnerUnit.setText(skuBean.getInnerUnit());
            tvWaitStackNum.setText("待上架：" + skuBean.getCaseCnt() + skuBean.getCaseUnit() + skuBean.getInnerCnt() + skuBean.getInnerUnit());
        }
    }

    @Override
    public void setScan(ScanResultBean scan) {
        if (scan.getType().equals("A")) {
            tvPallet.setText(scan.getCode());
        }
        if (scan.getType().equals("B")) {
            tvStorageBin.setText(scan.getCode());
        }

    }

    @Override
    public void onSuccess() {
        tvStorageBin.setText("");
        tvPallet.setText("");
        etNum.setText("");
    }

    @OnClick({R.id.tv_sku, R.id.tv_move_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_sku:
                PopUtisl.showSelectPop(mContext, skuBeanList, tvSku, new PopUtisl.SelectSkuClick() {
                    @Override
                    public void onClick(SkuBean bean) {
                        skuBean = bean;
                        init();
                    }
                }, "请选择");
                break;
            case R.id.tv_move_out:
                if (TextUtils.isEmpty(tvStorageBin.getText())) {
                    showMessage("作业库位不能为空！");
                    return;
                }
                if (TextUtils.isEmpty(tvPallet.getText())) {
                    showMessage("托盘不能为空！");
                    return;
                }
                if (TextUtils.isEmpty(etNum.getText())) {
                    showMessage("数量不能为空！");
                    return;
                }
                AffirmDialog affirmDialog=new AffirmDialog(mContext, new AffirmDialog.OnConfrim() {
                    @Override
                    public void onConfrim() {
                        HandingInRequest request = new HandingInRequest();
                        request.setEntryNo(entryNo);
                        request.setSku(skuBean.getSku());
                        request.setWorkStorageBinId(tvStorageBin.getText().toString());
                        request.setPallet(tvPallet.getText().toString());
                        if (!TextUtils.isEmpty(etNum.getText().toString())){
                            request.setInboundQuantity(Integer.parseInt(etNum.getText().toString()));
                        }
                        if (!TextUtils.isEmpty(etNumJian.getText().toString())){
                            request.setInQuantity(Integer.parseInt(etNumJian.getText().toString()));
                        }
                        presenter.manualShelve(request);
                    }
                });
                affirmDialog.show("确定开始上架？");
                break;
        }
    }
}
